<?php declare(strict_types=1);
/**
 * @author      guosir
 * @copyright   2023-2025 owner
 **/

namespace Srv\Apps\Apis\Order;

use Srv\Apps\Apis\Log\Ctl\LogCtl;
use Srv\Apps\Apis\Order\Ctl\PayCtl;
use Srv\Apps\Proto\DTO\Order\PayGoodsAddDTO;
use Srv\Apps\Proto\DTO\Order\PayGoodsEditDTO;
use Srv\Apps\Proto\DTO\Order\PayGoodsListDTO;
use Srv\Apps\Proto\Enum\ErrCodeEnum;
use Srv\Apps\Proto\Enum\LangKeyEnum;
use Srv\Apps\Proto\Enum\Log\LogResultEnum;
use Srv\Apps\Proto\VO\Order\PayGoodsListVO;
use Srv\Libs\ConfModel\RouteConfData;
use Srv\Libs\Frame\ActionAbstract;
use Srv\Libs\Frame\DTOAbstract;
use Srv\Libs\Frame\Lang;
use Srv\Libs\Frame\Page;
use Srv\Libs\Frame\ResponseData;
use Srv\Libs\Frame\VOAbstract;

final class PayAct extends ActionAbstract
{
    /**
     * @url /pay/goods/list
     * @auth payGoodsList
     * @req Srv.Apps.Proto.DTO.Order.PayGoodsListDTO
     * @rsp Srv.Apps.Proto.VO.Order.PayGoodsListVO
     * 支付产品列表
     */
    public function getPayGoodsList(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        if(!($DTOClass instanceof PayGoodsListDTO) || !($VOClass instanceof PayGoodsListVO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $Page                   = new Page();
        $currPage               = max(1, $DTOClass->getCurrPage());
        $pageSize               = $DTOClass->getPageSize() < 1 ? 12 : $DTOClass->getPageSize();
        $Page->init(0, $pageSize, $currPage);
        $whereData              = $RouteConfData->getParamsResData($DTOClass);
        $VOClass                = PayCtl::tag()->getPayGoodsList($Page, $whereData);
        return ResponseData::rspSuccAuto('', $VOClass, []);
    }

    /**
     * @url /pay/goods/add
     * @auth payGoodsAdd
     * @req Srv.Apps.Proto.DTO.Order.PayGoodsAddDTO
     * 支付产品新增
     */
    public function payGoodsAdd(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        unset($VOClass);
        if(!($DTOClass instanceof PayGoodsAddDTO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $setData    = $RouteConfData->getParamsResData($DTOClass);
        $LogCtl     = LogCtl::tag();
        $logData    = $setData;
        $pgspId     = PayCtl::tag()->payGoodsAdd($setData);
        if($pgspId < 1){
            $errMsg = '支付产品新增失败';
            $LogCtl->logAdd(LogCtl::PAY_GOODS_ADD, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $errMsg     = '支付产品新增成功';
        $LogCtl->logAdd(LogCtl::PAY_GOODS_ADD, LogResultEnum::LOG_RESULT_SUCCESS, $errMsg, $logData);
        return ResponseData::rspSuccAuto('', null, []);
    }

    /**
     * @url /pay/goods/edit
     * @auth payGoodsEdit
     * @req Srv.Apps.Proto.DTO.Order.PayGoodsEditDTO
     * 支付产品编辑
     */
    public function payGoodsEdit(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        unset($VOClass);
        if(!($DTOClass instanceof PayGoodsEditDTO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $pgspId             = intval($DTOClass->getPgspId());
        $setData            = $RouteConfData->getParamsResData($DTOClass);
        $PayCtl             = PayCtl::tag();
        $LogCtl             = LogCtl::tag();
        $logData            = $setData;
        $payGoodsInfoSrc    = $PayCtl->getPayGoodsSrc($pgspId);
        if(!isset($payGoodsInfoSrc['pgsp_id'])){
            $errMsg         = '支付产品不存在';
            $LogCtl->logAdd(LogCtl::PAY_GOODS_EDIT, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $result             = $PayCtl->payGoodsEdit($pgspId, $setData);
        $logData            = ['new' => $setData, 'old' => $payGoodsInfoSrc];
        if(!$result) {
            $errMsg         = '支付产品编辑失败';
            $LogCtl->logAdd(LogCtl::PAY_GOODS_EDIT, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $errMsg             = '支付产品编辑成功';
        $LogCtl->logAdd(LogCtl::PAY_GOODS_EDIT, LogResultEnum::LOG_RESULT_SUCCESS, $errMsg, $logData);
        return ResponseData::rspSuccAuto('', null, []);
    }
}